/*
 * // Bristleback plugin - Copyright (c) 2010 bristleback.googlecode.com
 * // ---------------------------------------------------------------------------
 * // This program is free software; you can redistribute it and/or modify it
 * // under the terms of the GNU Lesser General Public License as published by the
 * // Free Software Foundation; either version 3 of the License, or (at your
 * // option) any later version.
 * // This library is distributed in the hope that it will be useful,
 * // but without any warranty; without even the implied warranty of merchantability
 * // or fitness for a particular purpose.
 * // You should have received a copy of the GNU Lesser General Public License along
 * // with this program; if not, see <http://www.gnu.org/licenses/lgpl.html>.
 * // ---------------------------------------------------------------------------
 */

package pl.bristleback.server.bristle.binding.resolvers;

import org.apache.log4j.Logger;

/**
 * This property resolver checks the type of the given value, returns the same object if value type is Integer or uses {@link Integer#parseInt(String)} otherwise.
 * <p/>
 * Created on: 2011-03-10 22:48:44 <br/>
 *
 * @author Wojciech Niemiec
 */
public class IntegerPropertyResolver implements PropertyValueResolver<Integer> {
  private static Logger log = Logger.getLogger(IntegerPropertyResolver.class.getName());

  /**
   * Extracts value to integer.
   *
   * @param value value to extract.
   * @return integer version of value or null if value cannot be extracted.
   */
  public Integer extractValue(Object value) {
    if (value instanceof Integer) {
      return (Integer) value;
    } else if (value instanceof String) {
      try {
        return Integer.parseInt((String) value);
      } catch (NumberFormatException e) {
        return null;
      }
    } else {
      return null;
    }
  }
}
